﻿using System;
using System.Text;
using Rsdn.Janus.Framework;

namespace GetAssemblyInfoForIS
{
    class Program
    {
        /// <summary>
        /// This program is used at compile-time by the Inno Setup Install Scripts.
        /// It copies the file properties of an assembly and writes that info a
        /// header file that the scripts use to make the installer match the program
        /// </summary>
        static void Main(string[] args)
        {
            try
            {
                String inputFile = args[0];
                String outputFile = args[1];
                System.Diagnostics.FileVersionInfo fileInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(inputFile);
                using (System.IO.TextWriter writer = new System.IO.StreamWriter(outputFile, false, Encoding.Default))
                {
                    writer.WriteLine("; Auto generated constant for Inno script");
                    writer.WriteLine("; Usage : AppVersion={#APP_VERSION}");
                    writer.WriteLine("; defined : ");
                    writer.WriteLine(";          VER_MAJOR, VER_MINOR, VER_BUILD, VER_REVISION, APP_VERSION, ");
                    writer.WriteLine(";          APP_DESCRIPTION, APP_COPYRIGHT, ");
                    writer.WriteLine(";          APP_GUID, APP_NAME, APP_PUBLISHER");

                    writer.WriteLine("#define VER_MAJOR \"" + fileInfo.FileMajorPart + "\"");
                    writer.WriteLine("#define VER_MINOR \"" + fileInfo.FileMinorPart + "\"");
                    writer.WriteLine("#define VER_BUILD \"" + fileInfo.FileBuildPart + "\"");
                    writer.WriteLine("#define VER_REVISION \"" + fileInfo.FilePrivatePart + "\"");
                    writer.WriteLine("#define APP_VERSION \"" + fileInfo.ProductVersion + "\"");

                    writer.WriteLine("#define APP_DESCRIPTION \"" + fileInfo.FileDescription + "\"");
                    writer.WriteLine("#define APP_COPYRIGHT \"" + fileInfo.LegalCopyright + "\"");

                    writer.WriteLine("#define APP_KEY_NAME \"Janus\"");
                    writer.WriteLine("#define APP_TEXT_NAME \"RSDN@HOME-Ex\"");
                    writer.WriteLine("#define APP_PUBLISHER \"Rsdn\"");
                    writer.WriteLine("#define APP_URL \"http://rsdn.ru/forum/janus/\"");

                    writer.WriteLine("#define APP_MUTEX \"" + SharedConstants.JanusRunningInstanceDetectionMutex + "\"");

                    // Don't touch it for janus! 
                    writer.WriteLine("#define APP_GUID \"{{BA0F48BD-0128-4EFE-BD69-C0DE82EBBB7A}}\"");
#if DEBUG
                    writer.WriteLine("#define APP_TARGET \"Debug_X86\"");
#else
                    writer.WriteLine("#define APP_TARGET \"Release_X86\"");
#endif
                    writer.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\n\n");
                Console.WriteLine("Usage: GetAssemblyInfoForIS.exe MyApp.exe MyAppVersionInfo.iss\n");
            }
        }
    }
}